package leetcode;

import java.util.ArrayDeque;

public class MyTree {

    //前序遍历
    //递归
    public void front(TreeNode root) {
        if (root != null) {
            mid(root.left);
            System.out.println(root.val);
            mid(root.right);
        }
    }

    //中序遍历 最简单的
    //递归版
    public void mid(TreeNode root) {
        if (root != null) {
            System.out.println(root.val);
            mid(root.left);
            mid(root.right);
        }
    }

    public void mid1(TreeNode root) {
        ArrayDeque<TreeNode> dict = new ArrayDeque<>();
        dict.add(root);
        while (!dict.isEmpty()) {
            TreeNode treeNode = dict.pollFirst();
            System.out.println(treeNode);
            if (treeNode.left != null)
                dict.addLast(treeNode.left);
            if (treeNode.right != null)
                dict.addLast(treeNode.right);
        }
    }

    //后序遍历
    //递归
    public void back(TreeNode root) {
        if (root != null) {
            mid(root.left);
            mid(root.right);
            System.out.println(root.val);
        }
    }
}
